//
//  WBComposeToolBarView.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/24.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit

// MARK: -  按钮对应枚举
enum WBComposeToolBarType: Int{
    // 图片
    case Picture = 1
    // @
    case Mention = 2
    // #
    case Trend = 3
    // 表情
    case Emoticon = 4
    // +
    case Add = 5
    
}

class WBComposeToolBarView: UIView {
    
    // MARK: -  判断是否是表情button
    var isEmoticon:Bool = false{
        didSet{
            if isEmoticon{
                
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: UIControlState.Highlighted)
                
            }else{

                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: UIControlState.Normal)
                emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: UIControlState.Highlighted)
            }
            
        }
    }
    
    // MARK: -  将表情button记录
    var emoticonButton: UIButton?

    
    // MARK: -  定义一个闭包
    var composeToolBarColsure: ((type: WBComposeToolBarType)->())?
    
    // MARK: -  重写init方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setUpUI()
    }

    // MARK: -  重写init方法时必须要实现的方法
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  按钮的点击事件
    @objc private func clickToolBarButton(sender: UIButton) {
        
        composeToolBarColsure?(type: WBComposeToolBarType(rawValue: sender.tag)!)
    }
    // MARK: -  设置UI
    private func setUpUI() {
        
        
        let pictureButton = addChildbutton(imageName: "compose_toolbar_picture", type: WBComposeToolBarType.Picture)
        let mentionButton = addChildbutton(imageName: "compose_mentionbutton_background", type: WBComposeToolBarType.Mention)
        let trendButton = addChildbutton(imageName: "compose_trendbutton_background", type: WBComposeToolBarType.Trend)
        emoticonButton = addChildbutton(imageName: "compose_emoticonbutton_background", type: WBComposeToolBarType.Emoticon)
        let addButton = addChildbutton(imageName: "compose_add_background", type: WBComposeToolBarType.Add)
        
 
        pictureButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.bottom.top.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.width.equalTo(trendButton)
            make.leading.equalTo(pictureButton.snp_trailing)
        }
        
        trendButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.width.equalTo(emoticonButton!)
            make.leading.equalTo(mentionButton.snp_trailing)
        }
        
        emoticonButton!.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.equalTo(self)
            make.width.equalTo(addButton)
            make.leading.equalTo(trendButton.snp_trailing)
        }
        
        addButton.snp_makeConstraints { (make) -> Void in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
            
        }
        
    }

    // MARK: -  添加按钮的公共方法
    private func addChildbutton(imageName imageName: String,type: WBComposeToolBarType) -> UIButton {
        
        let button = UIButton()
        button.tag = type.rawValue
        button.addTarget(self, action: "clickToolBarButton:", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }
    
    
}
